package com.jintian.smart.platform.system.service;

import com.jintian.smart.base.api.system.entity.HelpCatalogEntity;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.platform.system.mapper.HelpCatalogMapper;
import com.jintian.smart.platform.system.mapper.HelpMapper;
import jakarta.annotation.Resource;
import com.jintian.smart.kernel.common.util.SmartBeanUtil;
import com.jintian.smart.base.api.system.form.HelpCatalogAddForm;
import com.jintian.smart.base.api.system.form.HelpCatalogUpdateForm;
import com.jintian.smart.base.api.system.view.HelpCatalogVO;
import com.jintian.smart.base.api.system.view.HelpVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 帮助文档 目录
 */
@Service
public class HelpCatalogService {
    @Resource
    private HelpCatalogMapper helpCatalogMapper;

    @Resource
    private HelpMapper helpMapper;

    /**
     * 查询全部目录
     *
     * @return
     */
    public List<HelpCatalogVO> getAll() {
        return SmartBeanUtil.copyList(helpCatalogMapper.all(), HelpCatalogVO.class);
    }

    /**
     * 添加目录
     *
     * @param helpCatalogAddForm
     * @return
     */
    public synchronized ResponseDTO<String> add(HelpCatalogAddForm helpCatalogAddForm) {
        List<HelpCatalogVO> helpDocCatalogList = getAll();
        Optional<HelpCatalogVO> exist = helpDocCatalogList.stream().filter(e -> helpCatalogAddForm.getName().equals(e.getName())).findFirst();
        if (exist.isPresent()) {
            return ResponseDTO.userErrorParam("存在相同名称的目录了");
        }

        helpCatalogMapper.insert(SmartBeanUtil.copy(helpCatalogAddForm, HelpCatalogEntity.class));
        return ResponseDTO.ok();
    }

    /**
     * 更新目录
     *
     * @param updateForm
     * @return
     */
    public synchronized ResponseDTO<String> update(HelpCatalogUpdateForm updateForm) {
        HelpCatalogEntity helpCatalogEntity = helpCatalogMapper.single(updateForm.getHelpDocCatalogId());
        if (helpCatalogEntity == null) {
            return ResponseDTO.userErrorParam("目录不存在");
        }

        List<HelpCatalogVO> helpDocCatalogList = getAll();
        Optional<HelpCatalogVO> exist = helpDocCatalogList.stream().filter(e -> updateForm.getName().equals(e.getName())).findFirst();
        if (exist.isPresent() && !exist.get().getHelpDocCatalogId().equals(updateForm.getHelpDocCatalogId())) {
            return ResponseDTO.userErrorParam("存在相同名称的目录了");
        }
        helpCatalogMapper.updateById(SmartBeanUtil.copy(updateForm, HelpCatalogEntity.class));
        return ResponseDTO.ok();
    }

    /**
     * 删除目录（如果有子目录、或者有帮助文档，则不能删除）
     *
     * @param helpDocCatalogId
     * @return
     */
    public synchronized ResponseDTO<String> delete(Long helpDocCatalogId) {
        if (helpDocCatalogId == null) {
            return ResponseDTO.ok();
        }

        HelpCatalogEntity helpCatalogEntity = helpCatalogMapper.single(helpDocCatalogId);
        if (helpCatalogEntity == null) {
            return ResponseDTO.userErrorParam("目录不存在");
        }

        //如果有子目录，则不能删除
        Optional<HelpCatalogVO> existOptional = getAll().stream().filter(e -> helpDocCatalogId.equals(e.getParentId())).findFirst();
        if (existOptional.isPresent()) {
            return ResponseDTO.userErrorParam("存在子目录：" + existOptional.get().getName());
        }

        //查询是否有帮助文档
        List<HelpVO> helpVOList = helpMapper.queryHelpDocByCatalogId(helpDocCatalogId);
        if (CollectionUtils.isNotEmpty(helpVOList)) {
            return ResponseDTO.userErrorParam("目录下存在文档，不能删除");
        }
        helpCatalogMapper.deleteById(helpDocCatalogId);
        return ResponseDTO.ok();
    }
}
